//
//  ProjectDetailHeaderView.m
//  DaDaBao
//
//  Created by mac on 15/8/14.
//  Copyright (c) 2015年 User. All rights reserved.
//

#import "ProjectDetailHeaderView.h"
#import "RoundProgress.h"
#import "NSMutableAttributedString+MutableString.h"
#import "ProjectDetailModel.h"

#define FONT(a) [UIFont systemFontOfSize:a]

#define PROMOT_COLOR_VALUE [UIColor hexStringToColor:@"#b9b9b9"]

#define BLACK_COLOR_VALUE [UIColor hexStringToColor:@"#3f3d3e"]

#define ORANGE_COLOR_VALUE [UIColor hexStringToColor:@"#f85303"]

@interface ProjectDetailHeaderView ()
{
    UILabel *promotLabel1;//投资人数
    UILabel *numLabel;//投资人数
    
    UILabel *promotLabel2;//投资人数
    UILabel *allMoneyLabel;//项目规模
    
    UILabel *promotLabel3;//投资人数
    UILabel *get_year_money_Label;//年化收益
    
    UILabel *promotLabel4;//投资人数
    UILabel *get_time_Label;//投资期限
    
    UILabel *lineLabel;//画线
    
    RoundProgress *roundProgress;//圆圈
    UILabel *rateLabel;//圈内数值label
    
    
    NSTimer *timer;
}

@property (nonatomic, strong) ProjectDetailModel *projectDetailModel;

@end

@implementation ProjectDetailHeaderView

//初始化
- (id)initWithFrame:(CGRect)frame andModel:(ProjectDetailModel *)projectDetailModel
{
    if (self = [super initWithFrame:frame]) {
        _projectDetailModel = projectDetailModel;
        [self initView];
        [self setValue];
    }
    return self;
}
- (void)initView
{
//    投资人数
    promotLabel1 = [[UILabel alloc] init];
    promotLabel1.textColor = PROMOT_COLOR_VALUE;
    promotLabel1.textAlignment = NSTextAlignmentCenter;
    promotLabel1.font = FONT(12.0f);
    [self addSubview:promotLabel1];
    
//  项目规模
    promotLabel2 = [[UILabel alloc] init];
    promotLabel2.textColor = PROMOT_COLOR_VALUE;
    promotLabel2.textAlignment = NSTextAlignmentCenter;
    promotLabel2.font = FONT(12.0f);
    [self addSubview:promotLabel2];
    
//   年化收益
    promotLabel3 = [[UILabel alloc] init];
    promotLabel3.textColor = PROMOT_COLOR_VALUE;
    promotLabel3.textAlignment = NSTextAlignmentCenter;
    promotLabel3.font = FONT(12.0f);
    [self addSubview:promotLabel3];
    
//  项目期限
    promotLabel4 = [[UILabel alloc] init];
    promotLabel4.textColor = PROMOT_COLOR_VALUE;
    promotLabel4.textAlignment = NSTextAlignmentCenter;
    promotLabel4.font = FONT(12.0f);
    [self addSubview:promotLabel4];
//  画圆
    roundProgress = [[RoundProgress alloc]initCreateTheCircleWithFrame:CGRectMake(0.0f, 0, 83.0f, 83.0f) withStartAngle:M_PI*3/2.0 andEndAngle:M_PI*3/2.0 andTheGrayCicleEndAngle:M_PI*3/2.0 +2.0*M_PI];
    roundProgress.center = CGPointMake(self.center.x, 80.0f);
    roundProgress.progressWidth = 4.0f;
    [self addSubview:roundProgress];
    
    rateLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 83.0f, 83.0f)];
    rateLabel.font = [UIFont systemFontOfSize:14.0f];
    rateLabel.textAlignment = NSTextAlignmentCenter;
//    rateLabel.text = @"42.00%";
    rateLabel.textColor = [UIColor hexStringToColor:RED_COLOR_VALUE];
    [roundProgress addSubview:rateLabel];
    
    
//    投资人数Number
    numLabel = [[UILabel alloc] init];
    numLabel.font = FONT(15.0f);
    numLabel.textColor = BLACK_COLOR_VALUE;
    numLabel.textAlignment = NSTextAlignmentCenter;
    [self addSubview:numLabel];
    
//    项目规模number
    allMoneyLabel = [[UILabel alloc] init];
    allMoneyLabel.font = FONT(15.0f);
    allMoneyLabel.textColor = BLACK_COLOR_VALUE;
    allMoneyLabel.textAlignment = NSTextAlignmentCenter;
    [self addSubview:allMoneyLabel];
   
//    年化收益number
    get_year_money_Label = [[UILabel alloc] init];
    get_year_money_Label.font = FONT(28.0f);
    get_year_money_Label.textColor = ORANGE_COLOR_VALUE;
    get_year_money_Label.textAlignment = NSTextAlignmentCenter;
    [self addSubview:get_year_money_Label];
   
//    项目期限number
    get_time_Label = [[UILabel alloc] init];
    get_time_Label.font = FONT(32.0f);
    get_time_Label.textColor = BLACK_COLOR_VALUE;
    get_time_Label.textAlignment = NSTextAlignmentCenter;
    [self addSubview:get_time_Label];
    
//    分割线
    lineLabel = [[UILabel alloc] init];
    lineLabel.backgroundColor = [UIColor lightGrayColor];
    [self addSubview:lineLabel];
    
}

#pragma mark- 坐标
- (void)layoutSubviews
{
    [super layoutSubviews];
    CGFloat labelWidth = self.frame.size.width/2;
    
    promotLabel1.frame = CGRectMake(0.0f, 78.0f, labelWidth, 12.0f);
    promotLabel2.frame = CGRectMake(labelWidth, 78.0f, labelWidth, 12.0f);
    promotLabel3.frame = CGRectMake(0.0f, 145.0f, labelWidth, 12.0f);
    promotLabel4.frame = CGRectMake(labelWidth, 145.0f, labelWidth, 12.0f);
    
    numLabel.frame = CGRectMake(0.0f, promotLabel1.frame.origin.y+promotLabel1.frame.size.height+10.0f, labelWidth, 15.0f);
    allMoneyLabel.frame = CGRectMake(labelWidth, promotLabel2.frame.origin.y+promotLabel2.frame.size.height+10.0f, labelWidth, 15.0f);
    get_year_money_Label.frame = CGRectMake(0.0f, promotLabel3.frame.origin.y+promotLabel3.frame.size.height+10.0f, labelWidth, 28.0f);
    get_time_Label.frame = CGRectMake(labelWidth, promotLabel4.frame.origin.y+promotLabel4.frame.size.height+10.0f, labelWidth, 32.0f);
    
    lineLabel.frame = CGRectMake(SCREEN_WIDTH/2, promotLabel3.frame.origin.y, 1.0f, 50.0f);
    
}

#pragma mark- 赋值
- (void)setValue
{
    promotLabel1.text = @"投资人数";
    promotLabel2.text = @"项目规模";
    promotLabel3.text = @"年化收益";
    promotLabel4.text = @"项目期限";
    
//   model取值赋值
    if ([_projectDetailModel.dueStatus intValue]!=0)
    {
    
        if([_projectDetailModel.saleStatus intValue] !=2)
        {
            if([_projectDetailModel.leftCredit intValue]!=0)
            {
            rateLabel.text = [NSString stringWithFormat:@"%.2f%@", [_projectDetailModel.progress doubleValue] * 100 / [_projectDetailModel.totalCredit doubleValue],@"%"];
            }
            else
            {
            rateLabel.text = @"已投满";
            }
        }
        
        else{
            rateLabel.text = @"停售";
        }

    }
    else
    {
        rateLabel.text = @"已过期";
    }
    numLabel.text = [NSString stringWithFormat:@"%@人",_projectDetailModel.investNumber];
    if([_projectDetailModel.totalCredit doubleValue] > 9999)
    {
        allMoneyLabel.text = [NSString stringWithFormat:@"%.0lf万元",[_projectDetailModel.totalCredit doubleValue] /10000.0f];
    }
    else
    {
        allMoneyLabel.text = [NSString stringWithFormat:@"%.0lf元",[_projectDetailModel.totalCredit doubleValue]];
    }
    
    get_year_money_Label.attributedText = [NSMutableAttributedString twoStringWithText1:[NSString stringWithFormat:@"%.2f",[_projectDetailModel.expectedReturnRatio floatValue]] text2:@"%" text1Color:ORANGE_COLOR_VALUE text2Color:ORANGE_COLOR_VALUE font1:FONT(28.0f) font2:FONT(18.0f)];
    get_time_Label.text = [NSString stringWithFormat:@"%@天",_projectDetailModel.timeLimit];
    
    [self performSelectorInBackground:@selector(createTimer) withObject:@"隐式创建"];//    开启分线程
}
//分线程方法，开定时器
-(void)createTimer
{
    if (![NSThread isMainThread]) {
        
        timer = [NSTimer scheduledTimerWithTimeInterval:0.001 target:self selector:@selector(startLoop) userInfo:nil repeats:TRUE];
        
        //将定时器添加到runloop中
        [[NSRunLoop currentRunLoop] addTimer:timer forMode:NSDefaultRunLoopMode];
        [[NSRunLoop currentRunLoop] run];
    }
}
//timer方法画圆
-(void)startLoop
{
    
    if (roundProgress.endAngle< M_PI*3/2.0 + [_projectDetailModel.progress doubleValue] / [_projectDetailModel.totalCredit doubleValue]*2.0*M_PI)//在这里更改圈的红色长度
    {
        roundProgress.endAngle += 0.05;
        [roundProgress setNeedsDisplay];
    }
    else
    {
        [timer invalidate];
        timer = nil;
    }
    
    
}
-(void)dealloc
{
    [timer invalidate];
    timer = nil;
}
@end
